#!/bin/bash
# PRISM="1" # forcing prism because it is required by the Category5 TV player.
# 
# 
# echo "11"; # Increase Progress Indicator
# echo "# Setting up repositories..."
function M_CHECK_REPO() #Checks repo file
{
cd ~
if [ ! -e ".perfectbuntu/sources50" ]       # Check if file exists.
  then
    echo This is apparently the first time you have run this script.
    echo
    echo Backing up your APT sources.list file to /etc/apt/sources.list.backup
    gksudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
    echo Done.
    echo
    echo Creating your APT entries now.
    mkdir ~/.perfectbuntu
    echo "" >> ~/.perfectbuntu/sources50
    echo "# Added by perfectbuntu - www.perfectbuntu.category5.tv" >> ~/.perfectbuntu/sources50
    echo "" >> ~/.perfectbuntu/sources50
 
# Avoid duplicates by using grep
    if grep 'main restricted universe multiverse'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
    echo "deb http://ca.archive.ubuntu.com/ubuntu/ "$OSVER" main restricted universe multiverse" >> ~/.perfectbuntu/sources50       
    fi 
 
    if grep 'main restricted'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
    echo "deb-src http://ca.archive.ubuntu.com/ubuntu/ "$OSVER" main restricted" >> ~/.perfectbuntu/sources50
    fi 
fi
}
# Medibuntu
#
# http://www.medibuntu.org/sources.list.d/karmic.list (Medibuntu Repository sample filename)
#
function R_MEDIBUNTU() # Medibuntu
{
if [ -e "/etc/apt/sources.list.d/medibuntu.list" ] ; then  # Check if file exists.
    if grep 'deb http://packages.medibuntu.org/ '$OSVER' free non-free' /etc/apt/sources.list.d/medibuntu.list > /dev/null; then
        echo "Keep existing /etc/apt/sources.list.d/medibuntu.list file from the same distro..."
    else
        echo "Removing existing /etc/apt/sources.list.d/medibuntu.list file from a different distro..."
        gksudo rm /etc/apt/sources.list.d/medibuntu.list
        dpkg -s medibuntu-keyring | grep "Status: install ok installed" > /dev/null 2>&1
        if [ $? = 0 ]; then
            echo "Removing existing medibuntu-keyring..."
            gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove medibuntu-keyring >> $LOGFILE"
        fi
    fi
fi
 
if [ -e "/etc/apt/sources.list.d/medibuntu.list" ] ; then  # Check if file still exists.
    echo "Omitting duplicate..."
else
    #echo "# deb http://packages.medibuntu.org/ "$OSVER" free non-free" >> ~/.perfectbuntu/sources50
    echo Retrieving Medibuntu Repository and GPG Signature...
    #// Medibuntu
    gksudo wget http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list \
    --output-document=/etc/apt/sources.list.d/medibuntu.list &&
    gksudo --message "$GKMSG" "apt-get -q update && >> $LOGFILE"
    gksudo --message "$GKMSG" "apt-get --yes -q --allow-unauthenticated install medibuntu-keyring && >> $LOGFILE"
    gksudo --message "$GKMSG" "apt-get -q update >> $LOGFILE"
    echo Done.
    echo
fi
}

function R_AWN() # Avant Window Navigator in Launchpad 
{
    if grep 'deb http://ppa.launchpad.net/awn-testing/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
    	echo "# Avant Window Navigator in Launchpad" >> ~/.perfectbuntu/sources50
    	echo "deb http://ppa.launchpad.net/awn-testing/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
	echo Retrieving GPG Signature...
	#// Avant Window Navigator
	gksudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BF810CD5
	echo Done.
	echo
    fi 
    if grep 'deb-src http://ppa.launchpad.net/awn-testing/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "deb-src http://ppa.launchpad.net/awn-testing/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
        echo "" >> ~/.perfectbuntu/sources50
    fi
# fi
}

function R_BACKINTIME() # Back in Time
{
    if grep 'deb http://le-web.org/repository stable main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "# Back in Time" >> ~/.perfectbuntu/sources50
        echo "deb http://le-web.org/repository stable main" >> ~/.perfectbuntu/sources50
        echo "" >> ~/.perfectbuntu/sources50
	echo Retrieving GPG Signature...
	#// Back In Time
        wget -q http://le-web.org/repository/le-web.key -O- | gksudo apt-key add -
    fi
# fi 
} 

function R_UBUNTUTWEAK() # Ubuntu-Tweak
{
    if grep 'deb http://ppa.launchpad.net/tualatrix/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "# Ubuntu-Tweak" >> ~/.perfectbuntu/sources50
        echo "deb http://ppa.launchpad.net/tualatrix/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
        echo Retrieving GPG Signature...
       #// Ubuntu-Tweak
        gksudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com FE85409EEAB40ECCB65740816AF0E1940624A220
        echo Done.
        echo
    fi
    if grep 'deb-src http://ppa.launchpad.net/tualatrix/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "deb-src http://ppa.launchpad.net/tualatrix/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
        echo "" >> ~/.perfectbuntu/sources50
    fi 
# fi
}

function R_GNOMEDO() # GNOME DO in Launchpad
{
    if grep 'deb http://ppa.launchpad.net/do-core/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
    	echo "# GNOME DO in Launchpad" >> ~/.perfectbuntu/sources50
    	echo "deb http://ppa.launchpad.net/do-core/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
	echo Retrieving GPG Signature...
	#// GNOME DO
	gpg --no-default-keyring --keyring /tmp/gnome-do.keyring --keyserver keyserver.ubuntu.com --recv A5D19FDCAA6ABB440CD3464628A8205077558DD0
	gpg --no-default-keyring --keyring /tmp/gnome-do.keyring --export --armor  A5D19FDCAA6ABB440CD3464628A8205077558DD0 | gksudo apt-key add -
	rm /tmp/gnome-do.keyring
	echo Done.
	echo
    fi 
    if grep 'deb-src http://ppa.launchpad.net/do-core/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "deb-src http://ppa.launchpad.net/do-core/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
        echo "" >> ~/.perfectbuntu/sources50
    fi
# fi
}

function R_MIRO() # Miro Internet TV
{
    if grep 'deb http://ftp.osuosl.org/pub/pculture.org/miro/linux/repositories/ubuntu '$OSVER'/'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "# Miro Internet TV" >> ~/.perfectbuntu/sources50
        echo "deb http://ftp.osuosl.org/pub/pculture.org/miro/linux/repositories/ubuntu $OSVER/" >> ~/.perfectbuntu/sources50
        echo "" >> ~/.perfectbuntu/sources50
    fi 
#fi
}

function R_MOOVIDA() # Moovida Media Center in Launchpad 
{
    if grep 'deb http://ppa.launchpad.net/moovida-packagers/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
    	echo "# MOOVIDA Media Center in Launchpad" >> ~/.perfectbuntu/sources50
    	echo "deb http://ppa.launchpad.net/moovida-packagers/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
	echo Retrieving GPG Signature...
	#// MOOVIDA
	gksudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 26C2E075
	echo Done.
	echo
    fi 
    if grep 'deb-src http://ppa.launchpad.net/moovida-packagers/ppa/ubuntu '$OSVER' main'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
        echo "deb-src http://ppa.launchpad.net/moovida-packagers/ppa/ubuntu "$OSVER" main" >> ~/.perfectbuntu/sources50
        echo "" >> ~/.perfectbuntu/sources50
    fi
# fi
}

function R_VIRTBOX() # VirtualBox 
{
    if grep 'deb http://download.virtualbox.org/virtualbox/debian '$OSVER' non-free'$ /etc/apt/sources.list > /dev/null; then
	echo "Omitting duplicate..."
    else
    	echo "# VirtualBox" >> ~/.perfectbuntu/sources50
    	echo "deb http://download.virtualbox.org/virtualbox/debian "$OSVER" non-free" >> ~/.perfectbuntu/sources50
    	echo "" >> ~/.perfectbuntu/sources50
    	echo Retrieving GPG Signature...
    	wget -q http://download.virtualbox.org/virtualbox/debian/sun_vbox.asc -O- | gksudo apt-key add -
	echo Done.
	echo
    fi 
}
 
function M_WRITE_REPO() #Writes repo file to right place
{
    cat ~/.perfectbuntu/sources50 | gksudo tee -a /etc/apt/sources.list
    echo Done.
    echo
#    fi
}

# echo "12"; # Increase Progress Indicator
# echo "# Cleaning up apt..."
function M_APT_CLEAN() # Cleaning up apt...
{
gksudo --message "$GKMSG" "apt-get clean >> $LOGFILE"
}

# echo "13"; # Increase Progress Indicator
# echo "# Updating apt..." 
function M_APT_UPDATE() # Updating apt...
{
gksudo --message "$GKMSG" "apt-get update >> $LOGFILE"
}
 
 
# #Run the installers
# 
# echo "14"; # Increase Progress Indicator
# echo "# Beginning installation procedures..."

function I_FLASH() #Flash plugin installer
{
	echo "# Closing your browser windows..."
	killall -9 firefox
	echo "# Removing your old Flash plugin..."
 	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m --purge remove flashplugin-nonfree adobe-flashplugin flashplugin-installer >> $LOGFILE"
	echo "# Installing Flash plugin installer..."
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install flashplugin-installer >> $LOGFILE"
}
function I_MULTIMEDIA() #multimedia codecs
{
	echo "# Installing multimedia codecs..."
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install ubuntu-restricted-extras >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install gstreamer0.10-plugins-ugly-multiverse gstreamer0.10-plugins-bad-multiverse gstreamer0.10-plugins-bad gstreamer0.10-plugins-ugly gstreamer0.10-ffmpeg libxine1-ffmpeg >> $LOGFILE"
	gksudo apt-get -y -f -m install "w"$BIT"codecs"
  if [[ $OSVER = 'karmic' ]]; then
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install libdvdread4 >> $LOGFILE"
  else
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install libdvdread3 >> $LOGFILE"
  fi
}
function I_FONTS() #Install billion fonts
{
	echo "# Installing roughly a billion fonts..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install msttcorefonts >> $LOGFILE"
	echo
	echo NOTE: That should be the last EULA you\'ll have to deal with.
	echo You can go get a coffee now while I finish up.
	echo It\'s going to take a while.
	echo
	echo Continuing with font installations...
	gksudo --message "$GKMSG" "apt-get -y -f -m install ttf-gentium ttf-dustin ttf-georgewilliams ttf-sjfonts sun-java6-fonts ttf-larabie-deco ttf-larabie-straight ttf-larabie-uncommon ttf-linux-libertine ttf-mgopen ttf-sil-charis ttf-sil-doulos ttf-ubuntu-title gsfonts-x11 ttf-fifthhorseman-dkg-handwriting ttf-alee ttf-alee ttf-ancient-fonts ttf-arhangai ttf-arphic-bkai00mp ttf-arphic-bsmi00lp ttf-arphic-gbsn00lp ttf-arphic-gkai00mp ttf-arphic-ukai ttf-atarismall ttf-baekmuk ttf-bengali-fonts ttf-beteckna ttf-bpg-georgian-fonts ttf-breip ttf-devanagari-fonts ttf-dzongkha ttf-ecolier-court ttf-essays1743 ttf-f500 ttf-farsiweb ttf-freefarsi ttf-gfs-artemisia ttf-gfs-bodoni-classic ttf-gfs-complutum ttf-gfs-didot-classic ttf-gfs-gazis ttf-gfs-neohellenic ttf-gfs-solomos ttf-gfs-theokritos ttf-gujarati-fonts ttf-inconsolata ttf-indic-fonts ttf-isabella ttf-junicode ttf-kacst ttf-kannada-fonts ttf-khmeros ttf-kiloji ttf-kochi-gothic-naga10 ttf-kochi-mincho-naga10 ttf-konatu ttf-liberation ttf-manchufont ttf-marvosym ttf-mikachan ttf-mona ttf-mph-2b-damase ttf-nafees ttf-ocr-a ttf-oflb-euterpe ttf-oriya-fonts ttf-paktype ttf-punjabi-fonts ttf-radisnoir ttf-sazanami-gothic ttf-sazanami-mincho ttf-sil-abyssinica ttf-sil-andika ttf-sil-ezra ttf-sil-padauk ttf-sil-scheherazade ttf-sil-yi ttf-staypuft ttf-summersby ttf-tamil-fonts ttf-tmuni ttf-tuffy ttf-unfonts ttf-unfonts-extra ttf-uralic ttf-vlgothic ttf-wqy-zenhei ttf-xfree86-nonfree ttf-xfree86-nonfree-syriac >> $LOGFILE"
	echo Done.
	echo
}
function I_DVD() #DVD decoders and support files
{
	echo "# Installing DVD decoders and support files..."
	if [[ $OSVER = 'karmic' ]]; then
	    gksudo apt-get -y -f -m install libdvdcss2 libdvdread4 ffmpeg "w"$BIT"codecs"
	else
	    gksudo apt-get -y -f -m install libdvdcss2 libdvdread3 ffmpeg "w"$BIT"codecs"
	fi
}
function I_PRELOAD() #Preload
{
	echo "# Installing preload..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install preload >> $LOGFILE"
}
function I_AUTOFSCK() #Autofck
{
	echo "# Installing AutoFsck..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install autofsck >> $LOGFILE"
}
function I_AWN() #Avant-window-navigator Dockbar
{
	echo "# Installing avant-window-navigator Dockbar..."
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install avant-window-navigator awn-applets-c-extras awn-manager >> $LOGFILE"
}
function I_COMPRESSION() #Multiple archive types in file-roller
{
	echo "# Installing support for multiple archive types in file-roller..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install lzma unrar rar p7zip p7zip-full p7zip-rar >> $LOGFILE"
}
function I_WINE() #WINE
{
	echo "# Installing WINE..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install wine >> $LOGFILE"
}
function I_SKYPE() #Skype
{
	echo "# Installing Skype..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install skype >> $LOGFILE"
}
function I_SPAM() #SPAM filters
{
	echo "# Installing SPAM filters..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install spamassassin >> $LOGFILE"
}
function I_CCSM() #Compiz Fusion
{
	echo "# Installing CCSM advanced visual effects for Compiz Fusion..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install compizconfig-settings-manager >> $LOGFILE"
	echo "# Installing Fusion Icon for Compiz Fusion..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install fusion-icon >> $LOGFILE"
}
function I_EWD() # Emerald Window Decorator
{
	echo "# Installing Emerald Window Decorator..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install emerald >> $LOGFILE"
}
function I_GNOMEDO() #GNOME Do application and file launcher
{
	echo "# Installing GNOME Do application and file launcher..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install gnome-do >> $LOGFILE"
}
function I_BACKINTIME() #Back In Time backup software
{
	echo "# Installing Back In Time backup software..."
	if [ "$ENVIRONMENT" = "kde" ]; then
		gksudo --message "$GKMSG" "apt-get -y -f -m install backintime-common backintime-kde4 >> $LOGFILE"
		echo Done.
	else if [ "$ENVIRONMENT" = "gnome" ]; then
		gksudo --message "$GKMSG" "apt-get -y -f -m install backintime-common backintime-gnome >> $LOGFILE"
		echo Done.
	else
		echo "# Failed installing Back In Time (no Gnome or KDE)"
		sleep 3
	fi
	fi
}
function I_UBUNTUTWEAK() #Ubuntu-tweak
{
	echo "# Installing ubuntu-tweak..."
		gksudo --message "$GKMSG" "apt-get -y -f -m install ubuntu-tweak >> $LOGFILE"
}
function I_MIRO() # Miro Internet TV
{
	echo "# Installing Miro Internet TV..."
        # No GPG Signature for Miro - Ignore unauthenticated error...
        gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m --allow-unauthenticated install miro >> $LOGFILE"
}
function I_VIRTBOX() #Virtualbox
{
	echo "# Removing old versions of VirtualBox..."
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox-3.2 >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox-3.1 >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox-3.0 >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox-2.2 >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox-2.1 >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox-2.0 >> $LOGFILE"
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m remove virtualbox >> $LOGFILE"
	
	echo "# Installing VirtualBox 4.0..."
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install virtualbox-4.0 >> $LOGFILE"
	echo "# Installing DKMS for VirtualBox 4.0..."
	gksudo --message "$GKMSG" "apt-get --force-yes -y -f -m install dkms >> $LOGFILE"
}
function I_PRISM() #Mozilla Prism application
{
	echo "# Installing Mozilla Prism application..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install prism >> $LOGFILE"
# fi
}
# Install stuff that everyone should have (no prompt)
function I_XNEST() #Allows you to connect to other Linux machines
{
	# xnest updates Terminal Server Client to support the XDMCP protocol allowing you to connect to other Linux machines.  It's strange this isn't included out-of-the-box.
	echo "# Installing support for XDMCP..."
	gksudo --message "$GKMSG" "apt-get -y -f -m install xnest >> $LOGFILE"
}
# # end of stuff everyone should have
# 
# zenity --title="Perfectbuntu $VER" --width $WIDTH --info --text="All done.  Enjoy the Perfect $OSNAME!\r\n\r\nhttp://www.perfectbuntu.category5.tv"
# 
# # Save log file
# zenity --ok-label="Yes" --cancel-label="No" --question --title="Perfectbuntu $VER" --text="Would you like to save a Perfectbuntu log file?";
# if [ $? == 0 ]
# then
# cp $LOGFILE `zenity --file-selection --save --confirm-overwrite --filename="perfectbuntu.log"`
# fi
# 
# echo "100"
# echo "# Perfectbuntu is finished."
# 
#   else
#     echo "100"
#     echo "# Perfectbuntu has been cancelled."
#     echo "User did not accept disclaimer.  Exiting." >> $LOGFILE
#     exit 1
#   fi

